﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Entities;
using BAL.Components;
using BAL.Utils;
using System.Data;
using DigitalAnts.App_Code.BAL.Components;
using VMCLibrary.RegexServiceProvider;

namespace DigitalAnts
{
    public partial class UserRegister : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Logged"] != null)
                {
                    if (Session["Logged"].Equals(true))
                    {
                        Response.Redirect("~/User/UserPage.aspx");
                    }
                }
                CountryList();

                this.ddtCountry.AppendDataBoundItems = true;
                this.ddtCountry.Items.Insert(0, new ListItem(" - - - Hãy chọn quốc gia - - - ", String.Empty));

                this.ddlCity.AppendDataBoundItems = true;
                this.ddlCity.Items.Insert(0, new ListItem(" - - - Hãy chọn quốc gia trước - - - ", String.Empty));
                this.ddlCity.Enabled = false;

                this.Title = "Đăng Ký Tài Khoản";
            }
          
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text.Equals(txtConfirmPassword.Text))
            {
                clsUser user = new clsUser();
                user.UserName = txtUserName.Text.Trim();
                RegexSpecificConfigManager validateUser = new RegexSpecificConfigManager();
                if (validateUser.ValidateCustomUserName("noname") == true)
                { }
                user.Password = clsSecurity.doEncryptPasswordWithMD5(txtPassword.Text.Trim());
                user.Email = txtEmail.Text.Trim();
                user.Address = txtAdress.Text.Trim();
                user.FirstName = txtFirstName.Text.Trim();
                user.LastName = txtLastName.Text.Trim();
                user.IdentityCard = txtIndentityCard.Text.Trim();
                user.PhoneNumber = txtPhoneNumber.Text.Trim();
                user.HomePhone = txtHomePhone.Text.Trim();
                user.BirthDay = Convert.ToDateTime(txtBirthDay.Text);
                user.CityID = Convert.ToInt32(ddlCity.SelectedValue);
                if (clsUserBAL.doUserRegister(user)==true)
                {
                    Response.Redirect("true");
                }
                else
                {

                }
            }

        }

        public void CountryList()
        {
            DataTable countrydt = clsCountryBAL.doGetCountry();
            ddtCountry.Items.Clear();
            this.ddtCountry.DataSource = countrydt;
            this.ddtCountry.DataTextField = countrydt.Columns["Country"].ColumnName.ToString();
            this.ddtCountry.DataValueField = countrydt.Columns["CountryID"].ColumnName.ToString();
            this.ddtCountry.DataBind();
        }

        public void CityList(clsCountry obj)
        {
            DataTable citydt = clsCountryBAL.doGetCity(obj);

            this.ddlCity.Items.Clear();
            if (citydt.Rows.Count > 0)
            {
                this.ddlCity.DataSource = citydt;
                this.ddlCity.DataTextField = citydt.Columns["CityName"].ColumnName.ToString();
                this.ddlCity.DataValueField = citydt.Columns["CityID"].ColumnName.ToString();
                this.ddlCity.DataBind();
                this.ddlCity.Enabled = true;
            }
            else {
                this.ddlCity.AppendDataBoundItems = true;
                this.ddlCity.Items.Insert(0, new ListItem(" - - - Hãy chọn quốc gia khác - - - ", String.Empty));
                this.ddlCity.Enabled = false;
            }
        }

        protected void ddtCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddtCountry.Items[0].Value == String.Empty)
            {
                ddtCountry.Items.RemoveAt(0);
                ddlCity.Items.RemoveAt(0);
            }
            clsCountry co = new clsCountry();
            co.CountryID = this.ddtCountry.SelectedValue;
            CityList(co);
        }
    }
}